<script setup>
import {
  Management,
  BellFilled,
  UserFilled,
  LocationFilled,
  Place,
  Memo,
  HomeFilled,
  PictureFilled,
  KnifeFork,
  Shop,
  Tickets,
  ChatDotRound,
  SwitchButton,
  CaretBottom
} from '@element-plus/icons-vue'

import avatar from '@/assets/default.png'
import { userInfoService } from '@/api/user.js'
import useUserInfoStore from '@/stores/userInfo.js'
import { useTokenStore } from '@/stores/token.js'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'

const tokenStore = useTokenStore()
const userInfoStore = useUserInfoStore()
const router = useRouter()

const getUserInfo = async () => {
  const user_data = userInfoStore.info
  const user_token = tokenStore.token
  if (!user_token || user_data.status != '1') {
    router.push('/login')
  }
}
getUserInfo()

const handleCommand = (command) => {
  if (command === 'logout') {
    ElMessageBox.confirm('您确认要退出吗?', '温馨提示', {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning'
    })
      .then(() => {
        tokenStore.removeToken()
        userInfoStore.removeInfo()
        router.push('/login')
        ElMessage({ type: 'success', message: '退出登录成功' })
      })
      .catch(() => {
        ElMessage({ type: 'info', message: '用户取消了退出登录' })
      })
  } else {
    router.push('/user/' + command)
  }
}
</script>

<template>
  <el-container class="layout-container">
    <el-aside width="200px">
      <div class="el-aside__logo"></div>
      <el-menu active-text-color="#ffd04b" background-color="#232323" text-color="#fff" router>
        <el-menu-item index="/log/list">
          <el-icon><Management /></el-icon>
          <span>系统日志</span>
        </el-menu-item>
        <el-menu-item index="/adminMessage">
          <el-icon><BellFilled /></el-icon>
          <span>通知管理</span>
        </el-menu-item>
        <el-sub-menu>
          <template #title>
            <el-icon><UserFilled /></el-icon>
            <span>景区中心</span>
          </template>
          <el-menu-item index="/jinqu/list">
            <el-icon><LocationFilled /></el-icon>
            <span>景区管理</span>
          </el-menu-item>
          <el-menu-item index="/adminJindian">
            <el-icon><Place /></el-icon>
            <span>景点管理</span>
          </el-menu-item>
          <el-menu-item index="/adminYuyue">
            <el-icon><Memo /></el-icon>
            <span>预约管理</span>
          </el-menu-item>
        </el-sub-menu>
        <el-menu-item index="/adminUserList">
          <el-icon><UserFilled /></el-icon>
          <span>用户管理</span>
        </el-menu-item>
        <el-menu-item index="/adminJiudian">
          <el-icon><HomeFilled /></el-icon>
          <span>酒店管理</span>
        </el-menu-item>
        <el-menu-item index="/adminImage">
          <el-icon><PictureFilled /></el-icon>
          <span>图片管理</span>
        </el-menu-item>
        <el-menu-item index="/adminXiaochi">
          <el-icon><KnifeFork /></el-icon>
          <span>特色小吃</span>
        </el-menu-item>
        <el-menu-item index="/adminDianpu">
          <el-icon><Shop /></el-icon>
          <span>店铺管理</span>
        </el-menu-item>
        <el-menu-item index="/adminYanpiao">
          <el-icon><Tickets /></el-icon>
          <span>验票</span>
        </el-menu-item>
        <el-menu-item index="/adminPinlun">
          <el-icon><ChatDotRound /></el-icon>
          <span>评论管理</span>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <el-container>
      <el-header>
        <div>青岛旅游平台后台管理系统：<strong>{{ userInfoStore.info.nickname }}</strong></div>
        <el-dropdown placement="bottom-end" @command="handleCommand">
          <span class="el-dropdown__box">
            <el-avatar :src="userInfoStore.info.userPic || avatar" />
            <el-icon><CaretBottom /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-header>

      <el-main>
        <router-view />
      </el-main>

      <el-footer>青岛旅游平台后台管理系统</el-footer>
    </el-container>
  </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;

  .el-aside {
    background-color: #232323;

    &__logo {
      height: 120px;
      background: url('@/assets/logoss.png') no-repeat center / 600px auto;
    }

    .el-menu {
      border-right: none;
    }
  }

  .el-header {
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .el-dropdown__box {
      display: flex;
      align-items: center;

      .el-icon {
        color: #999;
        margin-left: 10px;
      }

      &:active,
      &:focus {
        outline: none;
      }
    }
  }

  .el-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: #666;
  }
}
</style>
